import variables from "@/styles/element-variables.scss";
import defaultSettings from "@/settings";
import jsbos from "@/utils/jsbos";

const {
  showSettings,
  tagsView,
  tagsIcon,
  showLanguage,
  showSearch,
  useCache,
  fixedHeader,
  sidebarLogo,
  themeClass,
  slideClass,
  layoutType,
  sysConfig,
} = defaultSettings;

const state = {
  theme: variables.theme,
  loginNeedCode:
    jsbos.storageGet("loginNeedCode") === null
      ? false
      : jsbos.storageGet("loginNeedCode"),
  showSettings: showSettings,
  tagsView:
    jsbos.storageGet("tagsView") === null
      ? tagsView
      : jsbos.storageGet("tagsView"),
  tagsIcon:
    jsbos.storageGet("tagsIcon") === null
      ? tagsIcon
      : jsbos.storageGet("tagsIcon"),
  showLanguage:
    jsbos.storageGet("showLanguage") === null
      ? showLanguage
      : jsbos.storageGet("showLanguage"),
  showSearch:
    jsbos.storageGet("showSearch") === null
      ? showSearch
      : jsbos.storageGet("showSearch"),
  useCache:
    jsbos.storageGet("useCache") === null
      ? useCache
      : jsbos.storageGet("useCache"),
  fixedHeader:
    jsbos.storageGet("fixedHeader") === null
      ? fixedHeader
      : jsbos.storageGet("fixedHeader"),
  sidebarLogo:
    jsbos.storageGet("sidebarLogo") === null
      ? sidebarLogo
      : jsbos.storageGet("sidebarLogo"),
  themeClass:
    jsbos.storageGet("themeClass") === null
      ? themeClass
      : jsbos.storageGet("themeClass"),
  slideClass:
    jsbos.storageGet("slideClass") === null
      ? slideClass
      : jsbos.storageGet("slideClass"),
  layoutType:
    jsbos.storageGet("layoutType") === null
      ? layoutType
      : jsbos.storageGet("layoutType"),
  sysConfig:
    jsbos.storageGet("sysConfig") === null
      ? sysConfig
      : jsbos.storageGet("sysConfig"),
};

const mutations = {
  CHANGE_SETTING: (state, { key, value }) => {
    if (state.hasOwnProperty(key)) {
      state[key] = value;
      jsbos.storageSet({
        [key]: value,
      });
    }
  },
};

const actions = {
  changeSetting({ commit }, data) {
    commit("CHANGE_SETTING", data);
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
